# static.py

# Possibly interesting values:
#     CONTENT_LENGTH:
#     CONTENT_TYPE: application/x-www-form-urlencoded
#     PATH_INFO: /hello/mom/and/dad.html
#     QUERY_STRING: this=value&that=too
#     REMOTE_ADDR: 127.0.0.1
#     REQUEST_METHOD: GET
#     SCRIPT_NAME:
#     wsgi.errors: <file>
#     wsgi.file_wrapper: <file>
#     wsgi.input: <file to read request body>
#     wsgi.multiprocess: False
#     wsgi.multithread: True
#     wsgi.run_once: False

import os
import webob
import webob.exc

Root_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))

Web_framework_dir = os.path.join(Root_dir, "files", "static")

Content_types = {
    'html': 'text/html',
    'js': 'text/javascript',
    'css': 'text/css',
    'gif': 'image/gif',
    'png': 'image/png',
    'jpeg': 'image/jpeg',
    'jpg': 'image/jpeg',
}

def serve(req):
    # Parse the path:
    path = req.path_info.lstrip('/')

    if not path:
        path = 'index.html'
    full_path = os.path.join(Web_framework_dir, path)
    suffix = path.rsplit('.', 1)[1]

    try:
        with open(full_path, 'rb') as f:
            data = f.read()
    except IOError:
        return webob.exc.HTTPNotFound(full_path)

    return webob.Response(request=req, status="200 OK",
                          content_type=Content_types[suffix],
                          body=data)

